/* 信号基础篇 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>


static void sig_handle(int sig, siginfo_t *info, void *context) 
{ 
    sigval_t sig_val = info->si_value;

    printf("接受到的实时信号signal: %d\n",sig);
    printf("伴随数据为: %d\n",sig_val.sival_int);
} 

int main(int argc,char **argv)
{
    /* 检验传参个数 */ 
    if (2 > argc) 
        exit(-1);

    int num; 
    struct sigaction sig = {0};
    sig.sa_flags = SA_SIGINFO;
    sig.sa_sigaction = sig_handle;

    num = atoi(argv[1]);

    /* 注册信号处理函数 */
    if(-1 == sigaction(num,&sig,NULL)) {
        perror("sigaction error");
        exit(-1);
    }

    for(;;) {
        usleep(1000 * 500);
    }

    exit(0);
}